#include "MsgAddTableEntry.h"
#include "MsgOK.h"
#include "MsgError.h"
#include "TableManagementDefs.h"

extern "C"
{
#include <config.h>
#include <epan/epan.h>
#include <epan/packet.h>
}

MsgAddTableEntry::MsgAddTableEntry(std::string data)
{
    mapType = static_cast<ProtoMappingType> (data[0]);
    
    unsigned i = 2;
    while (data[i])
        i++;

    port = atol(data.substr(2, i - 1).c_str());

    dissectorName = data.substr(i + 1);
}


POutputMessage MsgAddTableEntry::Process(Model *model)
{
    dissector_handle_t dissector = 
            find_dissector(dissectorName.c_str());
    
    if(dissector != NULL)
    {
        dissector_change(MappingTypeNames[mapType].c_str(), 
                         port, 
                         dissector
                         );
        
        return POutputMessage(new MsgOK());
    }
    else
    {
        return POutputMessage(new MsgError(ERR_UNKNOWN_PROTOCOL, *model, 0));
    }
}
